package cn.lrwjz.androidguide.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import cn.lrwjz.androidguide.R;
import cn.lrwjz.androidguide.model.GridItem;

public class GridAdapter extends RecyclerView.Adapter<GridAdapter.GridViewHolder> {
    private List<GridItem> items;

    public GridAdapter(List<GridItem> items) {
        this.items = items;
    }

    @NonNull
    @Override
    public GridViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_grid, parent, false);
        return new GridViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull GridViewHolder holder, int position) {
        GridItem item = items.get(position);
        holder.imageView.setImageResource(item.getImageResId());
        holder.titleText.setText(item.getTitle());
    }

    @Override
    public int getItemCount() {
        return items.size();
    }

    static class GridViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView titleText;

        GridViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.imageView);
            titleText = itemView.findViewById(R.id.titleText);
        }
    }
} 